/* eslint-disable */

const perfix = "http://118.31.35.160:4000";

const http = {
  get(url, params) {
    if (params) {
      url += "?";
      const arr = [];
      for (const key in params) {
        arr.push(`${key}=${params[key]}`);
      }
      url += arr.join("&");
    }
    return new Promise(resolve => {
      return fetch(perfix + url, {
        headers: new Headers({
          Authorization: `Bearer ${localStorage.getItem("token")}`
        })
      })
        .then(response => response.json())
        .then(res => {
          if (res.status === 0) {
            resolve(res);
          } else {
            alert(res.msg);
          }
        });
    });
  },
  post(url, data) {
    return new Promise(resolve => {
      return fetch(perfix + url, {
        method: "POST", // or 'PUT'
        body: JSON.stringify(data), // data can be `string` or {object}!
        headers: new Headers({
          "Content-Type": "application/json",
          Authorization: `Bearer ${localStorage.getItem("token")}`
        })
      })
        .then(response => response.json())
        .then(res => {
          if (res.status === 0) {
            resolve(res);
          }
        });
    });
  }
};

export default http;
